﻿var menuedit = {
    formDefine: {
        labelWidth: 55, inputWidth: 245, space: 10, labelAlign: 'right',
        fields: [
                    { display: lang.zhname + "：", name: "MENU_NAME", type: "text", newline: true, width: 280, serviceDataName: 'MENU_NAME' },
                    { display: lang.enname + "：", name: "ENMENU_NAME", type: "text", newline: true, width: 280, serviceDataName: 'ENMENU_NAME' },
        //                    { display: lang.sign + "：", name: "MENU_TITLE", type: "text", newline: true, serviceDataName: 'MENU_TITLE' },
                    {display: lang.location + "：", name: "PARENTID", type: "selecttree", newline: true, dataoptions: "panelHeight:170" },
                    { display: lang.object + "：", name: "TARGET", type: "select", newline: true, data: 'data_MenuOpenType', serviceDataName: 'TARGET' },
                    { display: lang.sequence + "：", name: "SORTCODE", type: "text", newline: true, width: 280, serviceDataName: 'SORTCODE' },
                    { display: lang.address + "：", name: "NAVIGATEURL", type: "textarea", newline: true, width: 280, serviceDataName: 'NAVIGATEURL' },
                    { display: lang.save, name: "search", type: "button", newline: true, nlstyle: "text-align:center", width: 280, click: function () {
                        menuedit.form.submit();
                    }
                    },
                    { display: lang.close, name: "close", type: "button", width: 280, click: function () {
                        parent.$.closePage();
                    }
                    }
                ],
        fieldsInit: function () { },
        onLoaded: function (data, form) {
            var _currentval = '';
            if (data && data.PARENTID != '') {
                _currentval = data.PARENTID;

            }
            getmenu(_currentval);
        },
        submitUrl: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        submitUrlType: "EditMenuByMenuID",
        submitCheck: function (params) {

            if (!params["MENU_NAME"] || params["MENU_NAME"] == "") {
                return lang.Menu_name_cannot_be_empty;
            }
            if (!params["ENMENU_NAME"] || params["ENMENU_NAME"] == "") {
                return lang.Menu_enname_cannot_be_empty;
            }
            if (!params["PARENTID"] || params["PARENTID"] == "") {
                return lang.node_location_cannot_be_empty;
            }
            if (!params["TARGET"] || params["TARGET"] == "") {
                return lang.The_connection_object_is_not_null;
            }
            if (!params["SORTCODE"] || params["SORTCODE"] == "") {

                return lang.sequence_cannot_be_empty;
            }
            if (!params["NAVIGATEURL"] || params["NAVIGATEURL"] == "") {
                return lang.The_connection_address_is_not_null;
            }
            if (isNaN(params["SORTCODE"])) {
               return lang.sequence_must_be_digital;
            }


            return "";
        },
        submitParams: function (params) {
            var p = {};
            if ($.getPara('menuid') && $.getPara('menuid') != '') {
                p.action = 1;
                p.MENU_ID = $.getPara('menuid');
            } else { p.action = 2; }

            p.PARENTID = params["PARENTID"];
            if (params["PARENTID"] == '0') {
                p.MENU_TYPE = 1;
            }
            else {
                p.MENU_TYPE = 2;
            }
            p.MENU_NAME = params["MENU_NAME"];
            p.ENMENU_NAME = params["ENMENU_NAME"];
            p.NAVIGATEURL = params["NAVIGATEURL"];
            p.TARGET = params["TARGET"];
            p.SORTCODE = params["SORTCODE"];
            return p;
        },
        submitSuccess: function () {
            if ($.getPara('menuid') && $.getPara('menuid') != '') {
                $.alert(lang.Menu_updated_successfully, null, function () { parent.menulist.search(); parent.$.closePage(); });
            } else {
                $.alert(lang.add_menu_success, null, function () { parent.menulist.search(); parent.$.closePage(); });
            }
        }
    },
    init: function () {
        if ($.getPara('menuid') != '') {
            this.formDefine.url = App_Config.TadongUrl + "/AjaxMappingHandler.aspx";
            this.formDefine.urlType = "GetMenuByMenuID";
            this.formDefine.params = function () { return $.getPara('menuid'); };
        }
        jsPageRender(this);
    }
};



//#region 加载菜单数据
function getmenu(_currentval) {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryMenuList",
        success: function (data) {
            if (data && data.length > 0) {
                var _dataarr = [];
                var rootobj = {};
                rootobj.id = 0;
                rootobj.text = lang.root;
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.id = data[i].MENU_ID;
                        if (lang.SysLanguage == "en") {
                            obj.text = data[i].ENMENU_NAME;
                        }
                        else {
                            obj.text = data[i].MENU_NAME;
                        }

                        getsubmenu(data, data[i].MENU_ID, obj);
                        _data.push(obj);
                    }
                }
                rootobj.children = _data;
                _dataarr.push(rootobj);
                $('#formidPARENTID').combotree({ multiple: false });
                $('#formidPARENTID').combotree('loadData', _dataarr);
                if (_currentval != '') {
                    $('#formidPARENTID').combotree('setValue', _currentval);
                }
            }

        }
    });

    function getsubmenu(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PARENTID == _pid) {
                    var obj = {};
                    obj.id = _d[i].MENU_ID;
                    if (lang.SysLanguage == "en") {
                        obj.text = _d[i].ENMENU_NAME;
                    }
                    else {
                        obj.text = _d[i].MENU_NAME;
                    }
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubmenu(_d, _sdata[j].MENU_ID, _sdata[j]);
                }
            }
        }
    }
}

//#endregion